#include "cAudioLayer.h"

cAudioLayer::cAudioLayer(void)
{
}

cAudioLayer::~cAudioLayer(void)
{
}

bool cAudioLayer::Init()
{
	FMOD_RESULT result;

	result = FMOD::System_Create(&m_System);		// Create the main system object.

	if (result != FMOD_OK)
	{
		cLog::Instance()->FmodError(result,"System_Create() error!");
		return FALSE;
	}

	result = m_System->init(100, FMOD_INIT_NORMAL, 0);	// Initialize FMOD.
	if (result != FMOD_OK)
	{
		cLog::Instance()->FmodError(result,"System->init() error!");
		return FALSE;
	}

	return true;
}

void cAudioLayer::Finalize()
{
	if(m_System)
	{
		//m_System->release();
		m_System=NULL;
	}
}

bool cAudioLayer::LoopMusic()
{
	FMOD_RESULT result = FMOD_OK;
	bool play = false;

	if(m_channelMain)
	{
		bool isPlaying;

		m_channelMain->isPlaying(&isPlaying);
		
		if(!isPlaying)
		{
			play = true;
		}
	}
	else
	{
		play = true;
	}

	if(play)
	{
	result = m_System->playSound(FMOD_CHANNEL_FREE, m_soundMain, true, &m_channelMain); // paused
	//ERRCHECK(result);
	result = m_channelMain->setVolume(0.1f);		// Set the volume while it is paused.
	//ERRCHECK(result);
	result = m_channelMain->setPaused(false);		// This is where the sound really starts.
	//ERRCHECK(result);
	}

	if(result==FMOD_OK)
		return true;
	else
		return false;
}

bool cAudioLayer::LoopSounds(cPersonaje* Player, cEnemyManager* EnemyManager)
{
	FMOD_RESULT result;

	result = LoopPlayer(Player);

	result = LoopEnemies(EnemyManager);

	if(result==FMOD_OK)
		return true;
	else
		return false;
}

FMOD_RESULT cAudioLayer::LoopPlayer(cPersonaje* Player)
{
	FMOD_RESULT result = FMOD_OK;

	//
	// Sound effects fpor the player
	//
	int playerStatus = Player->GetDirection();

	switch(playerStatus)
	{
	case ATTACKR:
	case ATTACKL:
		Play(m_sDragonFist, &m_cDragonFist);
		break;
	case HITR:
	case HITL:
		Play(m_sDragonHit, &m_cDragonHit);
		break;
	case DIE:
		Play(m_sDragonDie, &m_cDragonDie);
		break;
	}

	return result;
}


FMOD_RESULT cAudioLayer::LoopEnemies(cEnemyManager* Enemies)
{
	FMOD_RESULT result = FMOD_OK;

	//
	// Sound effects for the enemies
	//
	int nEnemies = Enemies->GetList()->size();

	for(int i =0; i <nEnemies; i++)
	{
		cEnemy* enemy = Enemies->GetList()->at(i);

		int enemyStatus = enemy->GetDirection();

		switch(enemyStatus)
		{
			case E_ATTACKR:
			case E_ATTACKL:
				if(enemy->GetSeq()==0)
					Play(m_sSword);
				break;
			case E_HITR:
			case E_HITL:
				//Play(m_sDragonHit, &m_cDragonHit);
				break;
			case E_DIER:
			case E_DIEL:
				Play(m_sManScream);
				break;
		}
	}

	return result;
}

void cAudioLayer::Play(FMOD::Sound* sound)
{
		FMOD_RESULT result = m_System->playSound(FMOD_CHANNEL_FREE, sound, false, NULL);
		
		if(result!=FMOD_OK)
			cLog::Instance()->FmodError(result,"PlaySound() error!");
}

void cAudioLayer::Play(FMOD::Sound* sound, FMOD::Channel** channel)
{
	bool play = false;

	if(*channel)
	{
		bool isPlaying;

		(*channel)->isPlaying(&isPlaying);
		
		if(!isPlaying)
		{
			play = true;
		}
	}
	else
	{
		play = true;
	}

	if(play)
	{
		FMOD_RESULT result = m_System->playSound(FMOD_CHANNEL_FREE, sound, false, channel);
		
		if(result!=FMOD_OK)
			cLog::Instance()->FmodError(result,"PlaySound() error!");
	}
}

bool cAudioLayer::LoadData()
{
	FMOD_RESULT result;
	cLog* Log = cLog::Instance();

	result = m_System->createStream("main_track.mp3", FMOD_DEFAULT, 0, &m_soundMain);		// FMOD_DEFAULT uses the defaults.  These are the same as FMOD_LOOP_OFF | FMOD_2D | FMOD_HARDWARE.
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"main_track.mp3");
		return false;
	}

	result = m_System->createSound("wyvern_attack.mp3",FMOD_DEFAULT,0,&m_sDragonFist);
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"wyvern_attack.mp3");
		return false;
	}

	result = m_System->createSound("wyvern_hit.mp3",FMOD_DEFAULT,0,&m_sDragonHit);
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"wyvern_hit.mp3");
		return false;
	}

	result = m_System->createSound("wyvern_die.wav",FMOD_DEFAULT,0,&m_sDragonDie);
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"wyvern_die.wav");
		return false;
	}

	result = m_System->createSound("sword.mp3",FMOD_DEFAULT,0,&m_sSword);
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"sword.mp3");
		return false;
	}

	result = m_System->createSound("man_scream.mp3",FMOD_DEFAULT,0,&m_sManScream);
	if(result!=FMOD_OK)
	{
		Log->FmodError(result,"man_scream.mp3");
		return false;
	}

	return true;
}

void cAudioLayer::UnLoadData()
{
	if(m_soundMain)
	{
		m_soundMain->release();
		m_soundMain = NULL;
	}

	if(m_sDragonFist)
	{
		m_sDragonFist->release();
		m_sDragonFist = NULL;
	}

	if(m_sDragonHit)
	{
		m_sDragonHit->release();
		m_sDragonHit = NULL;
	}

	if(m_sDragonDie)
	{
		m_sDragonDie->release();
		m_sDragonDie = NULL;
	}

	if(m_sSword)
	{
		m_sSword->release();
		m_sSword = NULL;
	}

	if(m_sManScream)
	{
		m_sManScream->release();
		m_sManScream = NULL;
	}
}